/**----------------------------------------
 * @description  动态成功检测
 * @author       lxc
 * @file         DynamicPointCheck.ts
 * @Date         2022-05-05
 * @version      1.0.0
 * ----------------------------------------
 */

import { LogClass } from "../LogClass";

export interface JudgeData {
    success: boolean;      // 成功状态
    startIndex: number;     // 所取的数据下标
    duration: number;      // 持续时间
    judgeIndex: number;    // 当前判断的index

}
/**
 * 动态判断一定时间内达到一定次数的达成情况
 */
export class DynamicPointCheck extends LogClass {
    /**
     * 当前判断标记
     */
    currJudgeIndex = 0;
    /**
     * 总的判断次数
     */
    totalJudge = 3;
    /**
     * 判断进度
     */
    progress = 0;
    /**
     * 判断间隔时间
     */
    judgeInterval = 0.3;
    /**
     * 每个间隔检查的成功次数
     */
    intervalJudgeNum = 5;
    /**
     * 判断成功时间
     */
    successTimes = [];
    /**
     * 
     */
    currJudgeData: JudgeData = null;
    /**
     * 阶段数据容器
     */
    judgeStates: Map<number, JudgeData> = new Map();

    constructor() {
        super()
        this.initJudgeStates();
    }
    /**
     * 初始化判断的状态数据
     */
    initJudgeStates() {
        this.printLog("initJudgeStates")
        this.judgeStates.clear();
        this.currJudgeIndex = 1;
        this.progress = 0;
        for (let i = 1; i <= this.totalJudge; i++) {
            let judgedata : JudgeData = {
                success : false,
                judgeIndex : i,
                startIndex : 0,
                duration : i > 1 ? this.judgeInterval : 0 
            }
            this.judgeStates.set(i,judgedata);
        }
        this.currJudgeData = this.judgeStates.get(this.currJudgeIndex)
    }
    /**
     * 新的判断成功时间
     * @param newTime 
     */
    newSuccessTime(newTime: number){
        this.printLog("newSuccessTime",newTime)

        this.successTimes.push(newTime)

        this.clearTimeOutCache(newTime);

        this.completeJudge(newTime);

        return this.progress;
    }
    /**
     * 清理超时数据
     */
    clearTimeOutCache(newTime){
        if(newTime - this.successTimes[0] > this.totalJudge * this.judgeInterval){
            this.printLog("clearTimeOutCache")
            let newStartIndex = 0
            for(let i=0;i<this.successTimes.length;i++){
                if(newTime - this.successTimes[i] < this.totalJudge * this.judgeInterval){
                    newStartIndex = i;
                    break;
                }
            }
            this.successTimes.splice(0,newStartIndex+1);
        }
    }
    /**
     * 是否在判断的间隔时间内
     * @param newTime
     */
    inJudgeDuration(newTime){
        if(this.currJudgeIndex + 1 <= this.totalJudge){
            if(newTime - this.successTimes[this.currJudgeData.startIndex] >= this.currJudgeData.duration){
                this.nextJudge();
            }
        }else{
            this.successTimes = []
            this.printLog("PointCheckDone!")
            gVar.UIMsgMgr.dispatchMsg(LocalMsg.PointTimeCheckDone);
        }
    }
    /**
     * 判断下一阶段，刷新当前判断数据
     */
    nextJudge(){
        this.currJudgeIndex += 1;
        this.currJudgeData = this.judgeStates.get(this.currJudgeIndex);
        this.currJudgeData.startIndex = this.successTimes.length
        this.printLog("into next judge :",this.currJudgeData.startIndex);
    }
    /**
     * 完成判断
     * @param newTime 
     */
    completeJudge(newTime){
        let startIndex = this.currJudgeData.startIndex;
        if(this.successTimes.length >= startIndex + this.intervalJudgeNum){
            if(this.currJudgeData.success){
                this.inJudgeDuration(newTime)
                return;
            }
            if(this.successTimes[startIndex + this.intervalJudgeNum -1] - this.successTimes[startIndex] <= this.judgeInterval){
                this.currJudgeData.success = true;
                this.updateProgress()
                this.inJudgeDuration(newTime)
                this.printLog("complate judge ones");
            }else{
                this.successTimes.splice(0,this.successTimes.length -1);
                this.initJudgeStates()
            }
        }else{
            if(newTime - this.successTimes[startIndex] <= this.judgeInterval){
                this.updateProgress()
            }else{
                this.successTimes.splice(0, this.successTimes.length-1)
                this.initJudgeStates()
            }
        }
    }
    /**
     * 刷新判断进度
     */
    updateProgress(){
        let currSuccessNum = this.successTimes.length - this.currJudgeData.startIndex;
        let alreadSuccessNum = (this.currJudgeIndex -1) * this.intervalJudgeNum
        let currPercent = (currSuccessNum + alreadSuccessNum) / (this.totalJudge * this.intervalJudgeNum)
        this.progress = Math.min(currPercent,1)
        this.printLog("progress :",this.progress)
    }
}